<template>
  <div>
    <query-table ref="param_query_table" :page-size="20" :columns="columns"
                 :append-buttons="appendQueryButtons" :query-fields="queryFields"
                 :detail-fields="infoFields" base-url="/settings/common_param_version"
                 show-index></query-table>
    <el-dialog
        title="复制"
        :visible.sync="detailDlgVisible"
        append-to-body
        :close-on-click-modal="false"
        width="50%">
      <custom-field-form :ref="'info-form'+timestamp" :fields="infoFields" :data="this.copyInfoDetail" columns="2"></custom-field-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success"  @click="copyProject">保存</el-button>
        <el-button  @click="detailDlgVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
  export default {
    name: "ParamList",
    data() {
      return {
        copyInfoDetail:{},
        detailDlgVisible:false,
        //展示列信息
        columns: [
          {id: 'name', name: '公共参数名称', align: 'left'},
          {id: 'createTime', name: '创建时间', width: 150},
          {id: 'createUser', name: '创建人', type: 'user', width: 90},
          {id: 'publishTime', name: '发布时间', width: 150},
          {id: 'publishUser', name: '发布人', type: 'user', width: 90},
          {
            id: 'state', name: '状态', component: {
              id: 'tag', getType: (val) => {
                if (val == 'publish') {
                  return 'success';
                }
              }
            }, width: 80, type:'meta',source:'param-version-state'
          },
          {
            id: 'operation', name: '操作', type: 'button', width: 330, children: [
              {
                text: '查看', click: (row) => {
                  this.openPage('/param/publish',row.id,{src:this.$route.fullPath})
                },
              },
              {
                text: '编辑', operation: 'edit', isHide: (data) => {
                  //如果可研参数录入及查看权限都没有，则隐藏
                   return !this.checkPermission('common_param:record') || data.state == 'publish';
                }
              },
              {
                text: '复制', operation: 'copy', click:(row) =>{
                  this.copyInfoDetail = JSON.parse(JSON.stringify(row));
                  this.copyInfoDetail.id = row.id;
                  this.copyInfoDetail.srcId = row.id;
                  let name = this.copyInfoDetail.name.trim();
                  this.copyInfoDetail.name.trim();
                  this.copyInfoDetail.name =name+  "(复制)"

                  this.detailDlgVisible = true;
                },isHide:()=>{return !this.checkPermission("common_param:record")}
              },
              {
                text: '取消发布', operation: 'unPublish'
                , isHide: (data) => {
                  return !this.checkPermission('common_param:record') || data.state != 'publish';
                }
                ,click:(row) =>{
                  this.unPublish(row);
                }
              },
              {
                text: '删除', type: 'danger', operation: 'delete', isHide: (data) => {

                  //如果可研参数录入及查看权限都没有，则隐藏
                   return !this.checkPermission('common_param:delete') || data.state == 'publish';
                }
              },
            ]
          },
        ],
        //查询条件字段
        queryFields: [
          {id: 'name', name: '版本名称', colSpan: 6},
          {id: 'state', name: '版本状态', component:{id:'meta-select',type:'param-version-state'}, colSpan: 5},
        ],
        appendQueryButtons:[
          {text:'新增',type:'success',operation:'create',isHide:()=>{return !this.checkPermission("common_param:record")}},
        ],
        // permission:'common_param:record',
        infoFields: [
          {
            id: 'name',
            name: '版本名称',
            rules:[{ required: true, message: "请输入名称", trigger: "blur" }],
            colSpan:24,
          },
          {
            id: 'remark',
            name: '备注',
            colSpan: 24,
            component: {
              id:'textarea',
              maxLength:200
            }
          }
        ],
      }
    },
    computed: {},
    created: function () {
    },
    methods: {
      copyProject(id){
        this.$post("/settings/common_param_version/copy",this.copyInfoDetail,(res)=> {
          this.$message.success("复制成功");
           this.$refs['param_query_table'].reload();
          this.detailDlgVisible=false
        })
      },

        /**
          * 取消发布版本
          */
        unPublish(row) {
          this.$confirm('是否取消发布此版本, 是否继续?', '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            const loading = this.openLoading("正在处理，请稍后");
            this.$post("/settings/common_param_version/unPublish", row, (response) => {
              this.$message.success('取消发布成功');
              this.$refs['param_query_table'].reload();
            }, (response) => {
              loading.close();
            });
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '取消操作'
            });
          });
        },
    },

  }
</script>
